#include "ultrasonic.h"

#include "Weight.h"
#include "includes.h"
#include <string.h> 
#include <stdio.h>  
#include <stdbool.h>
#include <IOdevice.h>
#include "xrj_protocol.h"
#include "usart1.h"
#include "usart2.h"
#include "usart3.h"
#include "usart4.h"
#include "usart5.h"

#include "includes.h"
#include <string.h> 
#include <stdio.h>  
#include <stdbool.h>
#include <IOdevice.h>
#include "xrj_protocol.h"
#include "Key.h"


#if 1
#define		GPIO_Pin_FULL_RIGHT			GPIO_Pin_10
#define		GPIO_Pin_FULL_LEFT			GPIO_Pin_11


#define 	Read_GPIO_Pin_FULL_RIGHT		(GPIOA->IDR  & GPIO_Pin_FULL_RIGHT) 
#define 	Read_GPIO_Pin_FULL_LEFT			(GPIOC->IDR  & GPIO_Pin_FULL_LEFT)

void Ultrasonic_Init(void)
{
	//===和串口RX1和RX4冲突
	GPIO_InitTypeDef  GPIO_InitStructure; 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_FULL_RIGHT;//PA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//浮空输入
	GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10 
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_FULL_LEFT;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_Init(GPIOC, &GPIO_InitStructure);		
}

UINT32 KeyChkBuf = 0;
UINT32 KeyCvtBuf = 0;
UINT32 KeyDebounce = 0;
UINT32 KeyOldBuf = 0;
void Ultrasonic_Conduct(void)
{
	UINT32 KeyInBuf = 0;	
	if (Read_GPIO_Pin_FULL_RIGHT == 0){
		KeyInBuf |= 0x01; 
	}
	if (Read_GPIO_Pin_FULL_LEFT == 0){
		KeyInBuf |= 0x02;
	}
	
	if (KeyInBuf == KeyChkBuf){
		KeyDebounce++;
		if (KeyDebounce>=10){
			KeyCvtBuf = KeyChkBuf;
		}else{
			KeyDebounce = 10;
		}
		
	}else{
		KeyChkBuf = KeyInBuf;
		KeyDebounce = 0;	
	}	
	
	if (KeyCvtBuf != KeyOldBuf){
		if ((KeyCvtBuf^KeyOldBuf)&0x01){
			if (KeyCvtBuf&0x01){
				XRJ_mutex_lock(&Device_Value.Lock);
				Device_Value.TRASH_CAPACITY_1_Report = 0;
				XRJ_mutex_unlock(&Device_Value.Lock);	
			}else{
				XRJ_mutex_lock(&Device_Value.Lock);
				Device_Value.TRASH_CAPACITY_1_Report = 2000;
				XRJ_mutex_unlock(&Device_Value.Lock);
			}
		}
		
		if ((KeyCvtBuf^KeyOldBuf)&0x02){
			if (KeyCvtBuf&0x02){
				XRJ_mutex_lock(&Device_Value.Lock);
				Device_Value.TRASH_CAPACITY_2_Report = 0;
				XRJ_mutex_unlock(&Device_Value.Lock);	
			}else{
				XRJ_mutex_lock(&Device_Value.Lock);
				Device_Value.TRASH_CAPACITY_2_Report = 2000;
				XRJ_mutex_unlock(&Device_Value.Lock);
			}	
		}	
		
		KeyOldBuf = KeyCvtBuf;
	}
}	


void Ultrasonic_Task(void *pdata)
{
	Ultrasonic_Init();
	while(1)
	{	
		OSTimeDlyHMSM (0,0,0,20);
		Ultrasonic_Conduct();
	}
}

#else
UINT8 CheckSum(UINT8 *buf, INT32 len)
{
UINT8 ret = 0;
INT32 i;
	for (i = 0; i < len; i++)
	{
		ret += buf[i];
	}
	return ret;
}	


INT32 Read_(UINT8 *buf, UINT32 len, UINT32 timeout)
{
	return 	USART1_Read(buf,len,timeout);;
}


//----满桶超声波检测
UINT8 Ultrasonic_Read_Buf[32];


void Ultrasonic_Task(void *pdata)
{
	INT32 ret = 0;
	UINT8 CheckSum = 0;
	INT32 CntOutTime_1 = 0;
	INT32 CntOutTime_2 = 0;
	memset(Ultrasonic_Read_Buf,0,20);
	while(1)
	{	
		OSTimeDlyHMSM (0,0,0,20);
		memset(Ultrasonic_Read_Buf,0,20);
		ret = USART1_Read_NoBlock(Ultrasonic_Read_Buf,sizeof(Ultrasonic_Read_Buf));

		if (ret == 4)
		{	
			if (Ultrasonic_Read_Buf[0] == 0xFF)			
			{
				CheckSum = Ultrasonic_Read_Buf[0]+Ultrasonic_Read_Buf[1]+Ultrasonic_Read_Buf[2];
				if (Ultrasonic_Read_Buf[3] == CheckSum)
				{
					CntOutTime_1 = 0;
					XRJ_mutex_lock(&Device_Value.Lock);
					Device_Value.TRASH_CAPACITY_1_Report = Ultrasonic_Read_Buf[1]*256+Ultrasonic_Read_Buf[2];
					XRJ_mutex_unlock(&Device_Value.Lock);		
				}		
		//		printf("Ultrasonic get id1 = [%d] id2 = [%d]\n",Ultrasonic_Read_Buf[1],Ultrasonic_Read_Buf[2]);				
			}
			
		}		
		
		CntOutTime_1++;
		if (CntOutTime_1 > 1000)
		{
			CntOutTime_1 = 1000;			
			XRJ_mutex_lock(&Device_Value.Lock);
			Device_Value.TRASH_CAPACITY_1_Report = -1;
			XRJ_mutex_unlock(&Device_Value.Lock);		
		}	
		
		memset(Ultrasonic_Read_Buf,0,20);
		ret = USART4_Read_NoBlock(Ultrasonic_Read_Buf,sizeof(Ultrasonic_Read_Buf));

		if (ret == 4)
		{	
			if (Ultrasonic_Read_Buf[0] == 0xFF)			
			{
				CheckSum = Ultrasonic_Read_Buf[0]+Ultrasonic_Read_Buf[1]+Ultrasonic_Read_Buf[2];
				if (Ultrasonic_Read_Buf[3] == CheckSum)
				{
					CntOutTime_2 = 0;
					XRJ_mutex_lock(&Device_Value.Lock);
					Device_Value.TRASH_CAPACITY_2_Report = Ultrasonic_Read_Buf[1]*256+Ultrasonic_Read_Buf[2];
					XRJ_mutex_unlock(&Device_Value.Lock);		
				}		
		//		printf("Ultrasonic get id1 = [%d] id2 = [%d]\n",Ultrasonic_Read_Buf[1],Ultrasonic_Read_Buf[2]);				
			}
			
		}	

		CntOutTime_2++;
		if (CntOutTime_2 > 1000)
		{
			CntOutTime_2 = 1000;
			XRJ_mutex_lock(&Device_Value.Lock);
			Device_Value.TRASH_CAPACITY_2_Report = -1;
			XRJ_mutex_unlock(&Device_Value.Lock);		
		}			
		
		
	}
}
#endif





